import requests
import pymongo


# 目标: 拿到电视剧名称和播放页面url 并存储到MongoDB

class aqyTvInfoSpider:
    url = 'https://mesh.if.iqiyi.com/portal/lw/videolib/data'

    headers = {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "origin": "https://www.iqiyi.com",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://www.iqiyi.com/",
        "sec-ch-ua-mobile": "?0",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
    }

    cookies = {
        "QC005": "01fadf55c026f1a4238a4e4b0ae95c89",
        "QC007": "https%3A%2F%2Fcn.bing.com%2F",
        "QP0037": "0",
        "curDeviceState": "width%3D1475%3BconduitId%3D%3Bscale%3D130%3Bbrightness%3Ddark%3BisLowPerformPC%3D0%3Bos%3Dbrowser%3Bosv%3D10.0.19044",
        "QC234": "05e8396705e3372c8a6b777e0930fb4f",
        "QC235": "c2ff645f07894d2fbf52a7fb75941279",
        "T00404": "b0b65ad1119db4d30a7a5990bffa0a41",
        "QP0034": "%7B%22v%22%3A16%2C%22dp%22%3A1%2C%22dm%22%3A%7B%22wv%22%3A1%7D%2C%22m%22%3A%7B%22wm-vp9%22%3A1%2C%22wm-av1%22%3A1%2C%22m4-hevc%22%3A1%7D%2C%22hvc%22%3Atrue%2C%22l%22%3A1%7D",
        "QC006": "9e004093714567c24e28dfcae14cb118",
        "QP0030": "1",
        "__dfp": "a0e47593965c8548619e881159cc9c7143fe99675cb2a01060b50e4fed09975480@1729310055851@1728014056851",
        "IMS": "IggQABj_oYC4BiomCiA3NTA0NzliNWQxOTI0NmRjOWI2MGU2ZjBhM2IwNTAwNxAAIgByJAogNzUwNDc5YjVkMTkyNDZkYzliNjBlNmYwYTNiMDUwMDcQAIIBBCICEBuKASQKIgogNzUwNDc5YjVkMTkyNDZkYzliNjBlNmYwYTNiMDUwMDc"
    }

    params = {
        "uid": "",
        "passport_id": "",
        "ret_num": "60",
        "version": "12.94.20141",
        "device_id": "01fadf55c026f1a4238a4e4b0ae95c89",
        "channel_id": "2",
        "page_id": "1",
        "session": "fa3d9820bd84ea91b46098889ff65ddb",
        "os": "",
        "conduit_id": "",
        "vip": "0",
        "auth": "",
        "recent_selected_tag": "内地;战争;古装;内地;古装;00年代;全部;古装;最热;综合",
        "ad": "[{\"lm\":\"5\",\"ai\":\"5\",\"fp\":\"6\",\"sei\":\"S5ac4f163390fca3afa052511505aa109\",\"position\":\"library\"}]",
        "adExt": "{\"r\":\"1.1.5-ares6-pure\"}",
        "dfp": "a0e47593965c8548619e881159cc9c7143fe99675cb2a01060b50e4fed09975480",
        "filter": "{\"mode\":\"11\",\"three_category_id_v2\":\"2289882683101933\"}"
    }

    def __init__(self):
        self.client = pymongo.MongoClient('127.0.0.1', 27017)
        self.collection = self.client['aqy']['tvinfo']

    @classmethod
    def getInfo(cls, page_num):
        cls.params['page_id'] = str(page_num)
        response = requests.get(cls.url, headers=cls.headers, cookies=cls.cookies, params=cls.params)

        return response.json()


    def parse_data(self,response):
        for d in response['data']:
            tv = dict()
            tv['title'] = d['display_name']
            if '!' in d['page_url']: # 发现url里面带有!的都是跳转到首页的,将其去除 让title与url 一一对应  有问题可以配合redis去重 参考芒果tv的案例
                continue
            tv['url'] = d['page_url']
            print(tv)
            self.collection.insert_one(tv)

    def main(self):
        for page in range(1,11): # 一页60 刚好600条数据
            response = self.getInfo(page)
            self.parse_data(response)




if __name__ == '__main__':
    aqy = aqyTvInfoSpider()
    aqy.main()


